package com.xgst.land.activity.check

import android.content.Intent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.xgst.land.R
import com.xgst.land.bean.PDQueryBoxBean
import com.xgst.land.bean.QuerySampleBean
import com.xgst.land.databinding.InventoryQueryLayoutBinding
import com.xgst.land.viewmodel.BuildOrderViewModel

class InventoryQueryActivity : BaseActivity<InventoryQueryLayoutBinding>(), OnItemClickListener {
    override fun viewBinding(): InventoryQueryLayoutBinding {
        return InventoryQueryLayoutBinding.inflate(layoutInflater)
    }

   // override fun getLayoutId() = R.layout.inventory_query_layout

    private lateinit var model:BuildOrderViewModel

    private lateinit var itemAdapter: InventoryQueryAdapter
    override fun init() {
        setTitle("查询内容")
        model = ViewModelProvider(this)[BuildOrderViewModel::class.java]
        val projectId = intent.getIntExtra("projectId",0)
        val sampleBoxName = intent.getStringExtra("sampleBoxName")
        val sampleCategoryId = intent.getStringExtra("sampleCategoryId")

        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = InventoryQueryAdapter().apply { itemAdapter = this }

        initClick()
        showProgress()
        model.findSample("$projectId",sampleBoxName,sampleCategoryId,{ list ->
            if(list?.size == 0){
                return@findSample
            }
            /*list?.let { alreadyList ->
                for (a in alreadyList.count() - 1 downTo 0){
                    sc@ for (i in list.count() - 1 downTo 0){
                        if(alreadyList[a].id == list[i].id){
                            alreadyList.removeAt(a)
                            list.removeAt(i)
                            break@sc
                        }
                    }
                }
            }*/
            itemAdapter.setNewInstance(list)
            dismissProgress()
        })
    }

    private fun initClick() {
        //添加
        binding.btnAdd.setOnClickListener {
            val list = itemAdapter.data.filter { it.isChecked }.apply { forEach { it.isChecked = false } }
            val json = GsonUtils.toJson(list)
            val intent = Intent()
            intent.putExtra("json",json)
            setResult(RESULT_OK,intent)
            finish()
        }
        //全选
        binding.cbAll.setOnClickListener{
            itemAdapter.data.forEach {
                it.isChecked = binding.cbAll.isChecked
            }
            if(binding.cbAll.isChecked){
                binding.tvCheckCount.text = "${itemAdapter.data.size}"
            }else{
                binding.tvCheckCount.text = "0"
            }
            itemAdapter.notifyDataSetChanged()
        }

        itemAdapter.setOnItemClickListener(this)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as QuerySampleBean.DataBean
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<QuerySampleBean.DataBean>
        val bean = list.find { !it.isChecked }
        if(bean == null){
            binding.cbAll.isChecked = true
        }else{
            if(binding.cbAll.isChecked){
                binding.cbAll.isChecked = false
            }
        }
        binding.tvCheckCount.text = "${list.filter { it.isChecked }.size}"
    }
}
